/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject.inputQuestion;

import java.math.BigDecimal;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;

import ru.irenproject.TestCheck;
import ru.irenproject.TestInputException;
import ru.irenproject.store.Stash;

public final class TextPattern extends AbstractPattern {
  public static final int MAX_SIZE = 200;
  public static final int MAX_PRECISION_SIZE = 10;

  @Stash private InputQuestionProtos.TextPattern.Builder $;

  @Inject TextPattern() {}

  @Override protected void initialize() {
    super.initialize();
    setCaseSensitive(false);
    setSpaceDelimited(true);
    setWildcard(true);
    setPrecision("0");
  }

  public boolean hasDefaultOptions() {
    return !caseSensitive() && spaceDelimited() && wildcard()
        && hasPrecision() && precision().equals("0");
  }

  @Override public String value() {
    return $.getValue();
  }

  @Override public void setValue(String value) {
    TestCheck.input(value.length() <= MAX_SIZE);
    $.setValue(value);
    post(ChangeEvent.INSTANCE);
  }

  @Override public int qualityPercent() {
    return $.getQualityPercent();
  }

  @Override protected void internalSetQualityPercent(int qualityPercent) {
    $.setQualityPercent(qualityPercent);
    post(ChangeEvent.INSTANCE);
  }

  public boolean caseSensitive() {
    return $.getCaseSensitive();
  }

  public void setCaseSensitive(boolean caseSensitive) {
    $.setCaseSensitive(caseSensitive);
    post(ChangeEvent.INSTANCE);
  }

  public boolean spaceDelimited() {
    return $.getSpaceDelimited();
  }

  public void setSpaceDelimited(boolean spaceDelimited) {
    $.setSpaceDelimited(spaceDelimited);
    post(ChangeEvent.INSTANCE);
  }

  public boolean wildcard() {
    return $.getWildcard();
  }

  public void setWildcard(boolean wildcard) {
    $.setWildcard(wildcard);
    post(ChangeEvent.INSTANCE);
  }

  public boolean hasPrecision() {
    return $.hasPrecision();
  }

  public String precision() {
    Validate.isTrue(hasPrecision());
    return $.getPrecision();
  }

  public void setPrecision(String precision) {
    int size = precision.length();
    TestCheck.input(size <= MAX_PRECISION_SIZE);
    for (int i = 0; i < size; ++i) {
      char c = precision.charAt(i);
      TestCheck.input(Character.isDigit(c) || (c == '.'));
    }

    BigDecimal p;
    try {
      p = new BigDecimal(precision);
    } catch (NumberFormatException e) {
      throw new TestInputException(e);
    }

    if (p.signum() == 0) {
      p = BigDecimal.ZERO;
    } else {
      p = p.stripTrailingZeros();
    }

    $.setPrecision(p.toPlainString());
    post(ChangeEvent.INSTANCE);
  }

  public void clearPrecision() {
    $.clearPrecision();
    post(ChangeEvent.INSTANCE);
  }

  @Override public void setOptionsFrom(Pattern source) {
    if (source instanceof TextPattern) {
      TextPattern p = (TextPattern) source;
      setCaseSensitive(p.caseSensitive());
      setSpaceDelimited(p.spaceDelimited());
      setWildcard(p.wildcard());
      if (p.hasPrecision()) {
        setPrecision(p.precision());
      } else {
        clearPrecision();
      }
    }
  }
}
